package com.zzld.modules.autoinn.action;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.abel533.entity.Example;
import com.zzld.core.utils.Base64Coder;
import com.zzld.core.utils.CookieUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.ProductPing;
import com.zzld.modules.autoinn.entity.Store;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.autoinn.service.IExpenseService;
import com.zzld.modules.autoinn.service.IProductPingService;
import com.zzld.modules.autoinn.service.IStoreService;
import com.zzld.modules.common.dto.CommonJSON;

@Controller
@RequestMapping("/weixin/f/autoinn/xc/ping")
public class WXUserPingController {
 
	@Autowired
	private IProductPingService productService;

	@Autowired
	private ICustomerService customerService;

	@Autowired
	private IStoreService storeService;

	@Autowired
	private IExpenseService expenseService;

	private String listurl = "/wx/autoinn/pinglist";
	private String addurl = "/wx/autoinn/addping";

	@RequestMapping(method = RequestMethod.GET)
	public String list(HttpServletRequest request, Model model) {
		String storeid = request.getParameter("storeid") == null ? "" : request
				.getParameter("storeid");

	
		
		if (!storeid.equals("")) {
			List<ProductPing> p = productService.selectbystoreid(storeid);
			model.addAttribute("ping", p.size() == 0 ? null : p);
			model.addAttribute("count", p.size());
		} else {
			model.addAttribute("ping", null);
		}
		return listurl;
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model, HttpServletRequest request) {
		String expenseid = request.getParameter("expenseid") == null ? ""
				: request.getParameter("expenseid").toString();
		model.addAttribute("expenseid", expenseid);
		return addurl;
	}

	@RequestMapping(value = "add/pro", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON add(HttpServletRequest request, Model model) {

		String openid = CookieUtils.getCookie(request, "autoinn_session");
		String wxaccount = CookieUtils.getCookie(request, "wx_session");
		try {
			openid = Base64Coder.decode(openid);
			wxaccount = Base64Coder.decode(wxaccount);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		/*String wxaccount="12312321";
		String openid="123";*/
		Customer customer = null;
		try {
			customer = customerService.findCustomer(wxaccount, openid);
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println(customer.getCustomerId());
		
		// 订单号
		//String expenseid ="XF581428997525890";
		String expenseid=request.getParameter("expenseid").toString();
		System.out.println(expenseid);
		// 商品id
		String proid = null;
		try {
			proid = expenseService.selectByPrimaryKey(expenseid).getProductid();
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		System.out.println(proid);
		System.out.println("进入添加评论方法2");

		Map<String, Object> store = storeService.selectkey(proid);

		ProductPing p = new ProductPing();
		p.setId(UuidUtil.uuid().toString());
		p.setComment_content(request.getParameter("content").toString());
		p.setComment_time(new Date());
		p.setStore_id(store.get("id").toString());
		//p.setStore_id("402880ed4c36277f014c3628d0a50001");
		p.setComment_order("100");
		 p.setComment_tor(customer.getNickname());
		//p.setComment_tor("小灰灰");
		p.setComment_val(Integer.parseInt(request.getParameter("xing") == null ? "0"
				: request.getParameter("xing").toString()));
		p.setExpense_id(expenseid);

		CommonJSON json = new CommonJSON();
		try {
			if (!p.getComment_content().toString().equals("")) {
				if (productService.save(p) > 0) {
					Store s = storeService.selectByPrimaryKey(store.get("id").toString());
					s.setComment_val(s.getComment_val() + p.getComment_val());
					storeService.updateByPrimaryKeySelective(s);
					json.setMessage("添加成功");
					json.setSuccess(true);
				}
			} else {
				json.setMessage("请填写评价内容!");
				json.setSuccess(false);
			}
		} catch (Exception e) {
			json.setMessage("添加失败");
			json.setSuccess(false);
		}
		return json;
	}

	

}
